package com.haredot.desensitize.enums;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum DesensitizeType {

    TEL, EMAIL, PASSWORD, ID_CARD,  CUSTOM;

    public static String tel(String tel, String symbol) {
        if (tel.matches("1[3-9]\\d{9}")) {
            return tel.substring(0, 3) + symbol.repeat(4) + tel.substring(7);
        }
        return tel;
    }

    public static String email(String email, String symbol, int symbolLength) {
        if (email.matches("\\w+@\\w+(\\.\\w+){1,2}")) {
            return symbol.repeat(symbolLength) + email.substring(email.indexOf("@"));
        }
        return email;
    }

    public static String password(String password) {
        return null ;
    }

    public static String idCard(String idCard, String symbol) {
        if (idCard.matches("\\d{17}[0-9Xx]")) {
            return idCard.charAt(0) + symbol.repeat(10) + idCard.charAt(17);
        }
        return idCard ;
    }

    public static String custom(String text, String regex, String symbol, int symbolLength, int group) {
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text) ;

        if (m.find()) {
            int start = m.start(group);
            int end = m.end(group) ;
            text = text.substring(0, start) + symbol.repeat(symbolLength) + text.substring(end);
        }
        return text ;
    }

}
